﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WeixinMP.Interfaces;
using Microhard.Core.DBUtility;
using Microhard.WeixinMP.DataAccess;
using Microhard.WeixinMP.DTO;
using Microhard.Core;
using Microhard.WeixinMP.Msg;
using Microhard.WeixinMP.Models;

namespace Microhard.WeixinMP.Impls
{
    public class FeedbackImpl : ICRMFeedback
    {
        public Core.DBUtility.ExecutedResult Add(Models.FeedbackModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardWXEntities db = new MicrohardWXEntities();
            var op = model.ToEntity();
            op.CreateTime = DateTime.Now;
            //获得benlongentityID
            var wxname = model.WeixinName;
            var mpuser = db.MPUsers.SingleOrDefault(s => s.OpenID == wxname);
            op.BelongEntityID = mpuser.BelongEntityID;

            try
            {
                db.Feedbacks.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, FeedbackModelMsg.AddFailed);
                Log.Error("ICRMFeedback.Add", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Update(Models.FeedbackModel model)
        {
            ExecutedResult result = new ExecutedResult();
            MicrohardWXEntities db = new MicrohardWXEntities();
            var entity = db.Feedbacks.SingleOrDefault(l => l.FeedbackID == model.FeedbackID);
            if (entity != null)
            {
                entity.Result = model.Result;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, FeedbackModelMsg.UpdateFailed);
                Log.Error("ICRMFeedback.Update", ex);
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ExecutedResult Delete(Models.FeedbackModel model)
        {
            throw new NotImplementedException();
        }

        public Models.FeedbackModel GetModelByID(Guid id)
        {
            MicrohardWXEntities db = new MicrohardWXEntities();
            var fb = db.Feedbacks.SingleOrDefault(i => i.FeedbackID == id);
            if (fb != null)
            {
                return fb.ToModel();
            }
            return null;
        }

        public Models.FeedbackModel GetModelByModel(Models.FeedbackModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.FeedbackModel> Query(Models.FeedbackModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            MicrohardWXEntities db = new MicrohardWXEntities();
            IEnumerable<Feedback> entities = from b in db.Feedbacks
                                             where b.BelongEntityID == this.scope.EmpInfo.CompanyID
                                             select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
                return pagedEntities.Select(m => m.ToModel()).ToList();
            return new List<FeedbackModel>();
        }

        public List<Models.FeedbackModel> Query(Models.FeedbackModel Model)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
